package com.zhu.tool_test.aop.aspect;

import com.zhu.tool_test.aop.anno.ResultOpre;
import core.Data;
import core.MultipartData;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @program: tool_test
 * @description: 返回值切面
 * @author: ggBall
 * @create: 2020-12-13 13:16
 **/
@Component
@Aspect
@Slf4j
public class ResultAspect {

    /**
     *  切点
     */
    @Pointcut("@annotation(com.zhu.tool_test.aop.anno.ResultOpre)")
    private void pointcut(){}

    /**
     * 通知
     * ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中,
     * 添加了
     * Object proceed() throws Throwable //执行目标方法
     * Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法
     * @param pjd
     * @param resultOpre
     * @return
     */
    @Around("pointcut() && @annotation(resultOpre)")
    private Data advice(ProceedingJoinPoint pjd, ResultOpre resultOpre) {
        try {
            System.out.println("前置通知。。。" );
            Data proceed = (Data)pjd.proceed();
            String data = proceed.getStringPart("data");
            System.out.println("data = " + data.toString());
            if ("111".equals(data)) {
                return new MultipartData().include("data","无数据");
            } else {
                return proceed;
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("pjd = " + pjd);
        System.out.println("resultOpre = " + resultOpre);
        System.out.println("后置通知。。。" );
        return null;
    }
}
